<?php
/**
 * Created by PhpStorm.
 * User: mt
 * Date: 2018/6/5
 * Time: 9:41
 */

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Student;
use App\Transformers\StudentTransformer;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function test(Request $request)
    {

        $file = $request->file('file');
        $ext = $file->extension();
        $filename = md5(time()).'.'.$ext;
        $file->storeAs('files', $filename, 'admin');
        return response()->json([
            'link' => $filename
        ])->setStatusCode(201);

    }

    public function faceSearch(Request $request)
    {
        if($request->input('results'))
        {
            $result = $request->input('results');
            if(count($result) > 0){
                 foreach($result as $value)
                 {
                     $ids = Student::where('face_token', $value['face_token'])->first();
                     if(!empty($ids)){
                         $data = item_model_transform($ids, new StudentTransformer());
                         return response()->json($data)->setStatusCode(201);
                     }
                 }
            }
            return response()->json([])->setStatusCode(404);
        }
    }
}